//
//  advertisesVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/23.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class advertisesVC: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    
    var modelList: [mySelfRecruitmentInfo]?{
        didSet {
            if self.modelList?.count != 0{
                self.initRightItem()
                self.tableView.reloadData()
            }
            
            
        }
        
    }
    var type: String!//分为求职和招聘必传
    var msg: regiestInfo?{
        didSet{
        
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        
        let cellNib = UINib(nibName: "JoinUsWantJobFooterDetailCell", bundle: nil)
        
        self.tableView.registerNib(cellNib, forCellReuseIdentifier: "JoinUsWantJobFooterDetailCell")
        
        self.getMyAdvertises()
        publicMethod.hiddleExtendCellForTableView(self.tableView)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    //MARK: initRightItem
    func initRightItem(){
        let shareBtn = UIButton(frame: CGRectMake(0, 0, 25, 25))
        shareBtn.setBackgroundImage(UIImage(named: "right_item")!, forState: UIControlState.Normal)
        shareBtn.addTarget(self, action: #selector(self.shareAction), forControlEvents:  UIControlEvents.TouchUpInside)
        let share = UIBarButtonItem(customView: shareBtn)
        self.navigationItem.rightBarButtonItem = share
    }
    
    func shareAction(){
       self.tableView.editing = !self.tableView.editing
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if let count = self.modelList?.count {
            return count
        }
        return 0
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if self.type == "求职" {
            return 118.0
        } else {
            return 100.0
        }
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if self.type == "求职" {
            let cell = tableView.dequeueReusableCellWithIdentifier("JoinUsWantJobFooterDetailCell", forIndexPath: indexPath) as! JoinUsWantJobFooterDetailCell
            
            let model = modelList![indexPath.row]
            
            cell.iv.sd_setImageWithURL(NSURL(string: (format: model.imgsrc)), placeholderImage: UIImage(named: "placeholder"))
            
            cell.titleLbl.text = String(format: "%@%@%@",model.type ,model.unittype , model.positiontype)
            cell.nameLbl.text = model.name
            cell.sexLbl.text = model.sex
            cell.ageLbl.text = model.age
            cell.liftLbl.text = model.lift
            cell.addressLbl.text = model.unitarea
            cell.payLbl.text = model.pay

            return cell
            
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("advertisesCell", forIndexPath: indexPath) as! advertisesCell
            let model = modelList![indexPath.row]
            
            cell.nameLbl.text = model.title
            cell.addressLbl.text = String(format: "%@-%@",model.unitarea, model.unitaddress)
            cell.salaryLbl.text = model.pay
            cell.requireLbl.text = model.lift
            cell.unittagLbl.text = model.unittag
            
            return cell
 
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let sb = UIStoryboard.init(name: "NewsList", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("JoinWantedDetailVC") as! JoinWantedDetailVC
        vc.id = Int(modelList![indexPath.row].id)

        if self.type == "招聘" {
            
            let vc = sb.instantiateViewControllerWithIdentifier("JoinUsRecruitDetailVC") as! JoinUsRecruitDetailVC
            
            vc.id = Int(modelList![indexPath.row].id)
            
            
        }
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    //删除某行cell
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,
                   forRowAtIndexPath indexPath: NSIndexPath){
        let model = self.modelList![indexPath.row]
        self.delegateAdvertises(String(model.id))
        
    }
    //是否打开可以编辑
    func tableView(tableView: UITableView,
                   canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool{
        return true
        
    }
    //是否可以滑动删除
    func tableView(tableView: UITableView,
                   canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool{
        return true
        
    }
    //编辑样式
    func tableView(tableView: UITableView,
                   editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle{
        
        return .Delete

    }
    //MARK:networking
    //获取招聘／求职信息
    func getMyAdvertises(){
        var urlStr = String(format: "%@user/my/recruitment",BaseUrl)//招聘
        if type == "求职" {
            urlStr = String(format: "%@user/my/job",BaseUrl)//求职
        }
        
        DataTool.mySelfRecruitment(urlStr,completionHandler: { (newModel) in
            guard let newDataes = newModel else {
                return
            }
            
            print(newDataes)
            self.modelList = newDataes
            
        })

    }
    //删除招聘求职信息
    func delegateAdvertises(id: String)  {
        DataTool.delegateMyAdvertises(id, completionHandler: { (newModel) in
            guard let newDataes = newModel else {
                return
            }
            print(newDataes)
            self.msg = newDataes
            self.tableView.reloadData()
            self.getMyAdvertises()
            SVProgressHUD.showSuccessWithStatus(self.msg!.msg)
           
            
        })

    }
    
}
